package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.AdvertisementDto;
import com.huaxiafinance.management.dto.request.ImgUrlDto;
import com.huaxiafinance.management.dto.request.QueryAdversitementDto;
import com.huaxiafinance.management.model.YfqAdvertisement;
import com.huaxiafinance.management.service.YfqAdvertisementService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.QiNiuUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by xieWei on 2017/11/07
 */
@Controller("yfqAdvertisementController")
@RequestMapping("/yfqAdvertisement")
public class YfqAdvertisementController {

  private static final Logger log = Logger.getLogger(YfqAdvertisementController.class);

  @Autowired
  private YfqAdvertisementService yfqAdvertisementService;

  /**
   * 广告列表
   */
  @RequestMapping(value = "/queryAdvertisements", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryAdvertisement(@ModelAttribute("dto") QueryAdversitementDto dto) {
    dto.setPage(dto.getPage() == null ? null : dto.getPage() * dto.getRows());
    MessageResult messageResult = new MessageResult();
    log.info("广告列表查询条件：" + dto);
    try {
      List<YfqAdvertisement> yfqAdvertisementList = yfqAdvertisementService
          .queryYfqAdversitement(dto);
      dto.setPage(null);
      messageResult.setRows(yfqAdvertisementService.queryYfqAdversitement(dto).size());
      messageResult.setData(yfqAdvertisementList);
      messageResult.setSuccessResult();
    } catch (Exception ex) {
      messageResult.setFailureResult();
      ex.printStackTrace();
    }
    log.info("广告列表查询结果：" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 添加广告信息
   */
  @RequestMapping(value = "/insertAdvertisement", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String insertAdvertisement(@Valid @RequestBody AdvertisementDto dto, BindingResult result) {
    log.info("添加广告信息请求参数" + dto);
    MessageResult messageResult = new MessageResult();
    if (result.hasErrors()) {
      messageResult.setFailureResult(result);
    } else {
      try {
        yfqAdvertisementService.insertYfqAdvertisement(dto);
        messageResult.setSuccessResult();
      } catch (Exception e) {
        log.error(e.getMessage(), e);
        messageResult.setFailureResult();
      }
    }

    log.info("添加广告信息返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  /**
   * 修改广告
   */
  @RequestMapping(value = "/updateAdvertisement", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateAdvertisement(@Valid @RequestBody AdvertisementDto dto, BindingResult result) {
    log.info("修改广告信息请求参数" + dto);
    MessageResult messageResult = new MessageResult();
    if (result.hasErrors()) {
      messageResult.setFailureResult(result);
    } else {
      try {
        yfqAdvertisementService.updateByPrimaryKey(dto);
        messageResult.setSuccessResult();
      } catch (Exception e) {
        log.error(e.getMessage(), e);
        messageResult.setFailureResult();
      }
    }
    log.info("修改广告信息返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 修改状态
   */
  @RequestMapping(value = "/updateState", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateState(String id, Integer state) {
    MessageResult messageResult = new MessageResult();
    int max = 7;
    //state 1 广告上架
    if (state == 1) {
      //上架广告进行判断，超过5条不予许
      YfqAdvertisement yfqAdvertisement = yfqAdvertisementService.selectYfqAdvById(id);
      Integer type = yfqAdvertisement.getLocationType();
      if (type == 30 || type == 40) {
        max = 7;
      }
      int n = yfqAdvertisementService.count(yfqAdvertisement.getLocationType());
      if (n >= max) {
        messageResult.setMsg("广告位已满，无法上架");
        return messageResult.getAsJSON();
      }
    } else {
      //下架的广告置顶
      yfqAdvertisementService.updateTop(id);
    }
    yfqAdvertisementService.updateState(id, state);
    messageResult.setSuccessResult();
    return messageResult.getAsJSON();
  }

  /**
   * 广告置顶
   */
  @RequestMapping(value = "/updateTop", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateTop(String id) {
    MessageResult messageResult = new MessageResult();
    yfqAdvertisementService.updateTop(id);
    messageResult.setSuccessResult();
    return messageResult.getAsJSON();
  }

  /**
   * 活动banner图片修改和添加
   *
   * @param url 图片的路径
   */
  @RequestMapping(value = "/updateOrAddImg", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateOrAddImg(@RequestBody ImgUrlDto url) {
    MessageResult messageResult = new MessageResult();
    try {
      yfqAdvertisementService.updateOrAddImg(QiNiuUtil.url(url.getUrl()));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("banner图片修改错误", e);
      messageResult.setFailureResult();
    }
    return messageResult.getAsJSON();
  }

  /**
   * 活动banner图片获得
   */
  @RequestMapping(value = "/getBannerImg", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getBannerImg() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(yfqAdvertisementService.getBannerImg());
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("banner图片查询内容", e);
      messageResult.setFailureResult();
    }
    return messageResult.getAsJSON();
  }

  /**
   * 攻略url回显
   */
  @RequestMapping(value = "/getRaidersUrl", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getRaidersUrl() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(yfqAdvertisementService.getRaidersUrl());
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("攻略url回显异常", e);
      messageResult.setFailureResult();
    }
    return messageResult.getAsJSON();
  }

  /**
   * 攻略url编辑
   */
  @RequestMapping(value = "/updateRaidersUrl", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateRaidersUrl(String url) {
    MessageResult messageResult = new MessageResult();
    try {
      yfqAdvertisementService.updateRaidersUrl(url);
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("攻略url回显异常", e);
      messageResult.setFailureResult();
    }
    return messageResult.getAsJSON();
  }


}
